import requests
from datetime import datetime

Weather_key = '085f6432158c4574b923ad792b4ef732'

# 获取当前天气地区id
def get_location():
    # 调整所在的区域
    location = '开封市'
    Weather_city_url = f'https://geoapi.qweather.com/v2/city/lookup?key={Weather_key}&location={location}'
    city_json = requests.get(Weather_city_url).json()
    city_code = city_json["code"]
    if city_code.__eq__("200"):
        for Weather_city in city_json["location"]:
            Weather_city_name = Weather_city["name"]
            if Weather_city_name.__eq__("龙亭"):
                city_id = Weather_city["id"]
                return city_id
    else:
        print("访问获取地区接口失败！")
        return "访问获取地区接口失败！"


# 获取当天天气，不过没有最高温度，最低温度
def get_Weather():
    city_id = get_location()
    Weather_url = f'https://devapi.qweather.com/v7/weather/now?key={Weather_key}&location={city_id}'
    Weather_json = requests.get(Weather_url).json()
    # print(Weather_json)
    Weather_code = Weather_json["code"]
    Weather_now = Weather_json["now"]
    if (Weather_code.__eq__("200")):
        # print(Weather_now)
        return Weather_now["temp"]


def max_min_weather():
    city_id = get_location()
    Weather_url = f'https://devapi.qweather.com/v7/weather/3d?key={Weather_key}&location={city_id}'
    Weather_json = requests.get(Weather_url).json()
    sunset = Weather_json["daily"][0]["sunset"]
    tempMax = Weather_json["daily"][0]["tempMax"]
    tempMin = Weather_json["daily"][0]["tempMin"]
    textDay = Weather_json["daily"][0]["textDay"]
    return {"sunset": sunset, "tempMax": tempMax, "tempMin": tempMin, "textDay": textDay}


# 距离时间
def days_since(year, month, day):
    since_date = datetime(year, month, day)
    return (datetime.now() - since_date).days


def clc_days_since():
    lover_day = days_since(2024, 5, 13)
    wedding_day = days_since(2025, 12, 10)
    baby_birth = days_since(datetime.now().year, 8, 5)
    my_birth = days_since(datetime.now().year, 4, 5)
    return {"lover_day": lover_day, "wedding_day": 0 - wedding_day, "baby_birth": 0 - baby_birth,
            "my_birth": 0 - my_birth}


# 天气预报推送函数
def push_weather():
    message = max_min_weather()
    send_Message = "## 🧸️璐璐的专属️天气提醒🧸️" + "\n" + "\n" \
                   + "☀️今天的天气  " + message["textDay"] + "\n" + "\n" \
                   + "🌡温度  " + message["tempMin"] + "~" + message["tempMax"] + "\n" + "\n"\
                   + "🌅天黑时间 " + message["sunset"] + "\n"
    return send_Message


def push_day():
    day_message = clc_days_since()
    message = "## 🚀璐璐和我的爱情故事" + "\n" \
              + "❤️ **今天是我们相爱的第** " + str(day_message["lover_day"]) + "天" + "\n" + "\n" \
              + "📝距离我们领证还有" + str(day_message["wedding_day"]) + "天" + "\n" + "\n" \
              + "🎂距离老婆的生日还有" + str(day_message["baby_birth"]) + "天" + "\n" + "\n" \
              + "🎂距离我自己的生日还有" + str(day_message["my_birth"]) + "天" + "\n"
    return message


# 推送函数
def push():
    url = "https://api.anpush.com/push/MV4WTWMUR4YXZM169N83FKP2Y0K1Y2"

    payload = {
        "title": "🌈今日爱心播报",
        "content": push_day() + push_weather(),
        "channel": "31363"
    }

    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }

    response = requests.post(url, headers=headers, data=payload)

    print(response.text)


if __name__ == '__main__':
    push()
